#include "lexer.h"
#include "lexerpackage.h"
#include "lexercomment.h"

#include "../typedef/token.h"

#include <assert.h>

char *LexerComment(char *ch, int *line, int *len, Lexer *plex, int size, int s)
{
	assert(ch);
	assert(line);
	assert(len);
	assert(plex);
	int state;
	state = s;
	Token *ptoken;
	ptoken = 0;
	while ((*len) < size) {
		switch (state)
		{
			case 0: {
				//注释
				switch (*ch)
				{
					//注释单行
					case '/': {
						state = 1;
						(*len)++;
						ch++;
						continue;
					}
						//注释多行
					case '*': {
						//继续读取字符,检查是否为package
						state = 2;
						(*len)++;
						ch++;
						continue;
					}
					default: {
						return 0;
					}
				}
				printf("错误的lexer comment状态机.状态:0\n");
				return 0;
			}
			case 1: {
				//单行注释
				if (ptoken == 0) {
					ptoken = TokenInit(TokenType_Comment);
					ptoken->str = ch;
					ptoken->len++;
					ptoken->line = *line;
				}
				switch (*ch)
				{
					//注释单行
					case '\n': {
						//结束
						LexerPush(plex, ptoken);
						return ch;
					}
					default: {
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
				}
				printf("错误的lexer comment状态机.状态:1\n");
				return 0;
			}
			case 2: {
				if (ptoken == 0) {
					ptoken = TokenInit(TokenType_Comment);
					ptoken->str = ch;
					ptoken->len++;
					ptoken->line = *line;
				}
				switch (*ch)
				{
					case '*': {
						state = 3;
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
						//注释多行
					case '\n': {
						(*line)++;
					}
					default: {
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
				}
				printf("错误的lexer comment状态机.状态:2\n");
				return 0;
			}
			case 3: {
				switch (*ch)
				{
					case '/': {
						ptoken->len--;
						LexerPush(plex, ptoken);
						return ch;
					}
					case '*': {
						ptoken->len++;
						(*len)++;
						ch++;
						continue;
					}
					case '\n': {
						(*line)++;
					}
					default: {
						state = 2;
						ptoken->len++;
						(*len)++;
						ch++;
						continue;
					}
				}
				printf("错误的lexer comment状态机.状态:3\n");
				return 0;
			}

		}

	}
	return 0;
}
